package com.dfhc.pub.service;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jcifs.smb.SmbFile;

import org.quickbundle.project.RmGlobalReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dfhc.ISystemConstant;
import com.dfhc.util.SmbFileUtil;
import com.dfhc.util.StringHelper;


/**
 * 共享文件管理服务
 * @author kang
 * */
@Service
//默认将类中的所有public函数纳入事务管理
@Transactional(readOnly = true)
public class SmbFileService {
	
	/**
	 * 写文件，到有用户名和密码保护的共享目录
	 * 
	 * @param localUrl 本地文件位置或者上传文件的地址
	 * @param remoteUrl 存放文件的共享目录
	 * @return Map 写入结果信息
	 * */
	public Map<String, Object> writeSubFile(String localUrl,String remoteUrl) {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (StringHelper.isEmpty(localUrl)) {
			resultMap.put(ISystemConstant.AJAX_STATUS,ISystemConstant.AJAX_RESULT_FAIL);
			resultMap.put(ISystemConstant.AJAX_MESSAGE, "文件名为空！");
			return resultMap;
		}
		
		@SuppressWarnings("unchecked")
		Map<String, Object> map = RmGlobalReference.get(ISystemConstant.SMB_MESSAGE);
		String address="";
		String user="";
		String password="";
		for(Map.Entry<String, Object> entry : map.entrySet()){
			if("地址".equals(entry.getValue())){
				address = entry.getKey();
			}
			if("用户名".equals(entry.getValue())){
				user = entry.getKey();
			}
			if("密码".equals(entry.getValue())){
				password = entry.getKey();
			}
		}
		address =  "smb://"+address+"/";
		remoteUrl = remoteUrl.replace(":", "").replace("\\", "/");
		remoteUrl = address+remoteUrl;
		
//		String address = "smb://"+PropertyUtil.getProperty("share.address")+"/";
//		remoteUrl = remoteUrl.replace(":", "").replace("\\", "/");
//		remoteUrl = address+remoteUrl;
//		String user =PropertyUtil.getProperty("share.user");
//		String password =PropertyUtil.getProperty("share.password");
		return SmbFileUtil.writeSubFile(localUrl, remoteUrl, user, password);
		
	}
	
	/**
	 * 写文件，到有用户名和密码保护的共享目录
	 * 
	 * @param localUrl 本地文件位置或者上传文件的地址
	 * @param remoteUrl 存放文件的共享目录
	 * @return Map 写入结果信息
	 * */
	public Map<String, Object> writeSubFile(InputStream  ins,
			String fileName,String remoteUrl) {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (StringHelper.isEmpty(fileName)) {
			resultMap.put(ISystemConstant.AJAX_STATUS,ISystemConstant.AJAX_RESULT_FAIL);
			resultMap.put(ISystemConstant.AJAX_MESSAGE, "文件名为空！");
			return resultMap;
		}
		
		@SuppressWarnings("unchecked")
		Map<String, Object> map = RmGlobalReference.get(ISystemConstant.SMB_MESSAGE);
		String address="";
		String user="";
		String password="";
		for(Map.Entry<String, Object> entry : map.entrySet()){
			if("地址".equals(entry.getValue())){
				address = entry.getKey();
			}
			if("用户名".equals(entry.getValue())){
				user = entry.getKey();
			}
			if("密码".equals(entry.getValue())){
				password = entry.getKey();
			}
		}
		address =  "smb://"+address+"/";
		remoteUrl = remoteUrl.replace(":", "").replace("\\", "/");
		remoteUrl = address+remoteUrl;
		
		
//		String address = "smb://"+PropertyUtil.getProperty("share.address")+"/";
//		remoteUrl = remoteUrl.replace(":", "").replace("\\", "/");
//		remoteUrl = address+remoteUrl;
//		String user =PropertyUtil.getProperty("share.user");
//		String password =PropertyUtil.getProperty("share.password");
		return SmbFileUtil.writeSubFile(ins,fileName, remoteUrl, user, password);
		
	}
	
	public void main(String[] args) {
		writeSubFile("D://QQ.sql", "/D/aaa/");
	}
	
	/**
	 * 读文件，获取有用户名和密码保护的共享目录下的文件列表
	 * 
	 * @param remoteUrl 存放文件的共享目录
	 * @return SmbFileList 文件列表
	 * */
	public List<SmbFile> readSubFileList(String remoteUrl) {
		if(StringHelper.isEmpty(remoteUrl)){
			return null;
		}
		
		@SuppressWarnings("unchecked")
		Map<String, Object> map = RmGlobalReference.get(ISystemConstant.SMB_MESSAGE);
		String address="";
		String user="";
		String password="";
		for(Map.Entry<String, Object> entry : map.entrySet()){
			if("地址".equals(entry.getValue())){
				address = entry.getKey();
			}
			if("用户名".equals(entry.getValue())){
				user = entry.getKey();
			}
			if("密码".equals(entry.getValue())){
				password = entry.getKey();
			}
		}
		address =  "smb://"+address+"/";
		remoteUrl = remoteUrl.replace(":", "").replace("\\", "/");
		remoteUrl = address+remoteUrl;
		
//		String address = "smb://"+PropertyUtil.getProperty("share.address")+"/";
//		remoteUrl = remoteUrl.replace(":", "").replace("\\", "/");
//		remoteUrl = address+remoteUrl;
//		String user =PropertyUtil.getProperty("share.user");
//		String password =PropertyUtil.getProperty("share.password");
		return SmbFileUtil.readSubFileList(remoteUrl, user, password);
	}

	/**
	 * 读文件，获取有用户名和密码保护的共享目录下的文件
	 * 
	 * @param remoteUrl 共享目录下的文件完整路径
	 * @return SmbFile 文件
	 * */
	public SmbFile readSubFile(String remoteUrl) {
		if(StringHelper.isEmpty(remoteUrl)){
			return null;
		}
//		String address = "smb://"+PropertyUtil.getProperty("share.address")+"/";
//		remoteUrl = remoteUrl.replace(":", "").replace("\\", "/");
//		remoteUrl = address+remoteUrl;
//		String user =PropertyUtil.getProperty("share.user");
//		String password =PropertyUtil.getProperty("share.password");
		
		@SuppressWarnings("unchecked")
		Map<String, Object> map = RmGlobalReference.get(ISystemConstant.SMB_MESSAGE);
		String address="";
		String user="";
		String password="";
		for(Map.Entry<String, Object> entry : map.entrySet()){
			if("地址".equals(entry.getValue())){
				address = entry.getKey();
			}
			if("用户名".equals(entry.getValue())){
				user = entry.getKey();
			}
			if("密码".equals(entry.getValue())){
				password = entry.getKey();
			}
		}
		address =  "smb://"+address+"/";
		remoteUrl = remoteUrl.replace(":", "").replace("\\", "/");
		remoteUrl = address+remoteUrl;
		
		return SmbFileUtil.readSubFile(remoteUrl, user, password);
	}
}
